#include "Pet.h"
//using namespace std;

Pet::Pet()
{//default constructor
}

Pet::Pet(string Name, string Dna, string Sex): name(name), dna(Dna), sex(Sex)
{//parametric constructor
}

Pet::~Pet()
{//default constructor
}
//Perform function
void Pet::Perform(string cmd, ostream& fout )throw (AppError)
{
    if(cmd == "name?")
    {
         fout << "My name is " << Pet::MyNameIs();
    }
    else if(cmd == "species?")
    {
         fout << "My species is " << Pet::MySpeciesIs();
    }
    else if(cmd == "sex?")
    {
         fout << "My sex is " << Pet::MySexIs();
    }
    else throw AppError("Invalid command", "Pet::Perform");
}
//My name is function
string Pet::MyNameIs()
{
    return name;
}
//My species is function
string Pet::MySpeciesIs()
{
    return dna;
}
//My sex is function
string Pet::MySexIs()
{
    return sex;
}
//Extract function
 void   Pet::Extract(ifstream& fin ) throw(TokenError)
	{
           string opentkn, closetkn;
           fin >> opentkn;
           if( opentkn != "Pet{" )
			   throw TokenError("Invalid Open Token!", "Pet::Extract(1)");
           Get( fin );
           fin >> closetkn;
           if( closetkn != "}Pet" )
			   throw TokenError("Invalid Close Token!", "Pet::Extract(2)");
    }
//Insert function
void Pet::Insert(ostream& fout)
{
           fout << " Pet{ ";
           Put( fout );
           fout << " }Pet ";

}
//Get functions
void Pet::Get( ifstream& fin) throw(TokenError)
	 {
           string label;
           fin  >> label;
		   if( label != "name:" )
			   throw TokenError("Invalid field label, name: expected", "Pet::Get(1)");
		   fin  >> name;
		   fin  >> label;
		   if( label != "dna:" )
			   throw TokenError("Invalid field label, dna: expected", "Pet::Get(2)");
		   fin  >> dna;
		   fin  >> label;
		   if( label != "sex:" )
			   throw TokenError("Invalid field label, sex: expected", "Pet::Get(3)");
		   fin  >> sex;

     }

void Pet::Put ( ostream& fout)
	 {
           fout << " name: "   << name
                << " dna: " << dna
                << " sex: "   << sex;
     }

    //FRIENDS
	ifstream& operator>>(ifstream& fin, Pet& obj) throw(TokenError)
	{
		obj.Extract(fin);
		return fin;
	}

	ostream& operator<<(ostream& fout, Pet& obj)
	{
		obj.Insert(fout);
		return fout;
	}

